package com.formiaomiao.demain.activity;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.util.MessageEvent;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.SetLeftBgUtils;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.util.UseGlideUtils;
import com.formiaomiao.demain.weight.LoadingDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.channels.FileChannel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class DrawerToggleBackgroundActivity extends PicActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_drawer_set)
    TextView tvDrawerSet;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_head2)
    ImageView ivHead2;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_drawer_recovery)
    TextView tvDrawerRecovery;
    @BindView(R.id.ll_draw_now)
    ImageView llDrawNow;
    @BindView(R.id.ll_draw_set)
    ImageView llDrawSet;
    @BindView(R.id.v_bg_now)
    View vBgNow;
    @BindView(R.id.v_bg_set)
    View vBgSet;

    LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer_toggle_background);
        ButterKnife.bind(this);
        ViewInit();
    }

    @Override
    public void formAlbum(String path) {
        LubanInit(path);
    }

    @Override
    public void formCamera(String path) {
        LubanInit(path);
    }

    @Override
    public void formCorp(String path) {
        LubanInit(path);
    }

    public void ViewInit() {
        tvClassName.setText("修改侧滑栏背景");
        loadingDialog = new LoadingDialog(this);
        SetLeftBgUtils.setLeft(this, llDrawNow, vBgNow);
        tvName.setText(StaticValues.nickname);
        tvName2.setText(StaticValues.nickname);
        if (StaticValues.userheads == null) {
            UseGlideUtils.setGlide(this, StaticValues.userhead, ivHead);
            UseGlideUtils.setGlide(this, StaticValues.userhead, ivHead2);
        } else {
            UseGlideUtils.setGlide(this, StaticValues.userheads, ivHead);
            UseGlideUtils.setGlide(this, StaticValues.userheads, ivHead2);
        }
    }

    public void LubanInit(String path) {
//        File destDir = new File(Environment.getExternalStorageDirectory() + "/demain/skin/leftbg");
//        if (copyFile(new File(getRealPathFromUri(Uri.parse("content://media" + path))), destDir.getAbsolutePath())) {
            PreferenceUtils.setString(DrawerToggleBackgroundActivity.this,
                    PreferenceValues.LEFTBG, getRealPathFromUri(Uri.parse("content://media" + path)));
            EventBus.getDefault().post(new MessageEvent("侧滑栏背景"));
            SetLeftBgUtils.setLeft(DrawerToggleBackgroundActivity.this, llDrawSet, vBgSet);
            ToastUtils.show(DrawerToggleBackgroundActivity.this, "设置成功");
//        } else {
//            ToastUtils.show(DrawerToggleBackgroundActivity.this, "设置失败");
//        }
        loadingDialog.dismiss();
    }

    private String getRealPathFromUri(Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 根据文件路径拷贝文件
     *
     * @param src      源文件
     * @param destPath 目标文件路径
     * @return boolean 成功true、失败false
     */
    public boolean copyFile(File src, String destPath) {
        boolean result = false;
        if ((src == null) || (destPath == null)) {
            return result;
        }
        File dest = new File(destPath + "/" + src.getName());
        if (dest != null && dest.exists()) {
            dest.delete(); // delete file
        }
        try {
            dest.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e);
        }

        FileChannel srcChannel = null;
        FileChannel dstChannel = null;

        try {
            srcChannel = new FileInputStream(src).getChannel();
            dstChannel = new FileOutputStream(dest).getChannel();
            srcChannel.transferTo(0, srcChannel.size(), dstChannel);
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return result;
        }
        try {
            srcChannel.close();
            dstChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @OnClick({R.id.iv_class_back, R.id.tv_drawer_set, R.id.tv_drawer_recovery})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_drawer_set:
                showPicPopWindow(tvDrawerSet, "/demain/img/", false);
                break;
            case R.id.tv_drawer_recovery:
                llDrawSet.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                PreferenceUtils.setString(this, PreferenceValues.LEFTBG, "");
                EventBus.getDefault().post(new MessageEvent("侧滑栏背景"));
                SetLeftBgUtils.setLeft(this, llDrawSet, vBgSet);
                break;
        }
    }
}
